package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.YhMember;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.YhPointsWithdrawalRules;
import com.ruoyi.system.domain.vo.YhMemberWithdrawalRecordsVo;
import com.ruoyi.system.service.IYhMemberService;
import com.ruoyi.system.service.IYhPointsWithdrawalRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YhMemberWithdrawalRecordsMapper;
import com.ruoyi.system.domain.YhMemberWithdrawalRecords;
import com.ruoyi.system.service.IYhMemberWithdrawalRecordsService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户提现记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-06
 */
@Service
public class YhMemberWithdrawalRecordsServiceImpl extends ServiceImpl<YhMemberWithdrawalRecordsMapper, YhMemberWithdrawalRecords> implements IYhMemberWithdrawalRecordsService
{

    @Autowired
    private YhMemberWithdrawalRecordsMapper yhMemberWithdrawalRecordsMapper;

    @Autowired
    private IYhMemberService yhMemberService;

    @Autowired
    private IYhPointsWithdrawalRulesService pointsWithdrawalRulesService;

    @Transactional
    @Override
    public boolean addWithdrawalRecords(YhMemberWithdrawalRecords yhMemberWithdrawalRecords) {
        String withdrawalSource = yhMemberWithdrawalRecords.getWithdrawalSource();
        Long userId = SecurityUtils.getUserId();

        if(StringUtils.isEmpty(withdrawalSource)){
            throw new ServiceException("非法的提现类型");
        }

        // 查询是否存在申请中的提现
        List<YhMemberWithdrawalRecords> list = this.list(new LambdaQueryWrapper<YhMemberWithdrawalRecords>()
                .eq(YhMemberWithdrawalRecords::getMemberId, userId)
                .eq(YhMemberWithdrawalRecords::getWithdrawalSource, yhMemberWithdrawalRecords.getWithdrawalSource())
                .eq(YhMemberWithdrawalRecords::getState, 1));
        if(list.size() > 0){
            throw new ServiceException("存在同类型待审核的提现申请。");
        }


        yhMemberWithdrawalRecords.setMemberId(userId.toString());
        // 初始化状态为申请中
        yhMemberWithdrawalRecords.setState(1);
        // 冻结用户积分或者佣金
        YhMember yhMember = yhMemberService.getById(userId);
        if("POINT".equals(withdrawalSource)){
            Double usePoint = yhMemberWithdrawalRecords.getUsePoint();
            if(usePoint <= 0){
                throw new ServiceException("非法的积分数量");
            }
            double sub = Arith.sub(yhMember.getPoint() == null ? 0 : yhMember.getPoint(), usePoint);
            if(sub < 0){
                throw new ServiceException("非法的积分数量");
            }
            yhMember.setPoint(sub);
            // 冻结积分
            yhMember.setFrozenPoint(usePoint);

            // 获取积分提现规则
            YhPointsWithdrawalRules pointsWithdrawalRules = pointsWithdrawalRulesService.getById(1);
            Double withdrawalRatio = pointsWithdrawalRules.getWithdrawalRatio();
            double price = Arith.divRoundDown(usePoint, withdrawalRatio, 2);
            yhMemberWithdrawalRecords.setWithdrawalPrice(price);

        }else if("COMMISSION".equals(withdrawalSource)) {
            // 申请的金额
            Double withdrawalPrice = yhMemberWithdrawalRecords.getWithdrawalPrice();
            if(withdrawalPrice <= 0){
                throw new ServiceException("非法的佣金金额");
            }
            double sub = Arith.sub(yhMember.getCommission() == null ? 0 : yhMember.getCommission(), withdrawalPrice);
            if(sub < 0){
                throw new ServiceException("非法的佣金金额");
            }
            // 冻结佣金
            yhMember.setCommission(sub);
            yhMember.setFrozenCommission(withdrawalPrice);
        }else {
            throw new ServiceException("非法的提现类型");
        }
        yhMemberService.updateById(yhMember);
        return this.save(yhMemberWithdrawalRecords);
    }

    @Transactional
    @Override
    public boolean withdrawalProcess(YhMemberWithdrawalRecords yhMemberWithdrawalRecords) {
        YhMemberWithdrawalRecords byId = this.getById(yhMemberWithdrawalRecords.getId());
        if(byId.getState() != 1){
            throw new ServiceException("提现申请状态异常，请刷新页面");
        }
        Integer state = yhMemberWithdrawalRecords.getState();
        String withdrawalSource = byId.getWithdrawalSource();
        YhMember yhMember = yhMemberService.getById(byId.getMemberId());
        if(state == 2){
            if("POINT".equals(withdrawalSource))
                yhMember.setFrozenPoint(0D);

            if("COMMISSION".equals(withdrawalSource))
                yhMember.setFrozenCommission(0D);

            byId.setState(2);
            byId.setRemarks("申请已通过");
        }else if(state == 3){
            if("POINT".equals(withdrawalSource)) {
                yhMember.setPoint(Arith.add(yhMember.getPoint(), yhMember.getFrozenPoint()));
                yhMember.setFrozenPoint(0D);
            }

            if("COMMISSION".equals(withdrawalSource)) {
                yhMember.setCommission(Arith.add(yhMember.getCommission(), yhMember.getFrozenCommission()));
                yhMember.setFrozenCommission(0D);
            }

            byId.setState(3);
            if(StringUtils.isEmpty(yhMemberWithdrawalRecords.getRemarks())) {
                byId.setRemarks("申请未通过，冻结" + (withdrawalSource.equals("POINT") ? "积分" : "佣金" + "已返还"));
            }
        }else{
            throw new ServiceException("非法的审核状态");
        }
        yhMemberService.updateById(yhMember);
        return this.updateById(byId);
    }

    @Override
    public List<YhMemberWithdrawalRecordsVo> withdrawalRecordsList(YhMemberWithdrawalRecords yhMemberWithdrawalRecords) {
        return yhMemberWithdrawalRecordsMapper.withdrawalRecordsList(yhMemberWithdrawalRecords.queryWrapper());
    }
}
